<?php


/** ensure this file is being included by a parent file */
defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );
global $mosConfig_absolute_path, $mosConfig_live_site;
if (!defined("_JEXEC")) { DEFINE('_JEXEC', '1'); }
if (!defined("JURI_BASE")) { DEFINE('JURI_BASE', $mosConfig_live_site); }
if (!defined("JPATH_ADMINISTRATOR")) { DEFINE('JPATH_ADMINISTRATOR', JPATH_BASE.DS.'administrator'); }

if ($isInstalled = modHelpDeskOpenHelper::_isInstalled())
{
	$includeFiles = modHelpDeskOpenHelper::_includeFiles();
	$html = modHelpDeskOpenHelper::_renderModule( $params );

	if ($html) 
	{ 
		echo "
		<div class='".$params->get('class_sfx')."'>
		<div>
		<div>
		<div>";
			if ($params->get('subtitle')) { echo "<h3>".$params->get('subtitle')."</h3>"; }
			echo $html; 
		echo "
		</div>
		</div>
		</div>
		</div>";
	}
}

/**
 * 
 * @return 
 */
class modHelpDeskOpenHelper 
{
	/**
	 * 
	 * @return unknown_type
	 */
	function _includeFiles()
	{
		static $included;
		
		if (empty($included))
		{
			global $mosConfig_absolute_path, $mosConfig_live_site;
            $lang = Jconfig::getInstance()->config_lang;			
			if (file_exists(JPATH_BASE.'/components/com_helpdesk/languages/'.$lang.'.php')) {
			      include_once(JPATH_BASE.'/components/com_helpdesk/languages/'.$lang.'.php');
			} else {
			      include_once(JPATH_BASE.'/components/com_helpdesk/languages/russian.php');
			}
			$included = '1';
		}
	}
	
	/**
	 * 
	 * @return unknown_type
	 */
	function _isInstalled()
	{
		$success = false;

		// Require Helpers
		$helpersPath = JPATH_ADMINISTRATOR.DS.'components'.DS.'com_helpdesk'.DS.'helpers';
		$dh  = opendir($helpersPath);
		while (false !== ($filename = readdir($dh))) 
		{
			$fullpath = $helpersPath.DS.$filename;
			$extension = pathinfo( $fullpath, PATHINFO_EXTENSION );
			if (!is_dir( $fullpath ) && file_exists( $fullpath ) && ($extension == 'php') ) 
			{
				require_once(JPATH_BASE.DS."components".DS."com_helpdesk".DS.'helpdesk.class.php');
				require_once( $fullpath );			
				$success = true;
			}
		}
			
		return $success;
	}
	
	/**
	 * Render Module
	 *
	 * @access public
	 * @return mixed Parameter value
	 * @since 1.5
	 */
	function _renderModule( $params ) 
	{
		$html = '';

		$itemid = HelpDeskHelperHelpDesk::getItemid();
		$helpdeskOpen = modHelpDeskOpenHelper::getHelpDeskOpen();
		$num = count($helpdeskOpen);
		$num = intval($num);

		$display_null = $params->get( 'display_null', '0' );
		$null_text = $params->get( 'null_text', 'No Tickets' );
		
		if ($num > 0) {		
			if ($num == 1) { $text = HelpDeskText::_( 'Open Ticket' ); } else { $text = HelpDeskText::_( 'Open Tickets' ); }			
		    $html .= '<div>';
				$link = "index.php?option=com_helpdesk&Itemid=".$itemid;
				$link = HelpDeskRoute::_( $link, false );
				$html .= "<a href='".$link."'>".$num." ".$text."</a>"; 
			$html .= '</div>';
		} elseif ($display_null == '1') {
			$text = HelpDeskText::_( $null_text );
		    $html .= '<div>';
				$link = "index.php?option=com_helpdesk&Itemid=".$itemid;
				$link = HelpDeskRoute::_( $link, false );
				$html .= "<a href='".$link."'>".$text."</a>"; 
			$html .= '</div>';
		}

		return $html;
	}

    /**
     * Retrieves data
     *
     * @access public
     */    
    function getHelpDeskOpen( ) 
    {
		global $mainframe;
		
		$database = HelpDeskConfig::getDBO();
		$user 	= HelpDeskConfig::getUser();
		
		// open tix
		$status_query = " AND ( #__helpdesk_tickets.status = '0' OR #__helpdesk_tickets.status = '3' ) ";
			  
		$query = "SELECT * FROM #__helpdesk_tickets "
			. " WHERE 1 "
			. $status_query
			. " AND #__helpdesk_tickets.sender_userid = '$user->id' "
			;	
		$database->setQuery( $query );
		$total = $database->loadObjectList();
		
		return $total;
    }
}